package com.eye.catcher.controller;


import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TYwDaObserveInfo;
import com.eye.catcher.entity.TYwDaTemperatureHumidityInfo;
import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.rabbit.MQDto;
import com.eye.catcher.service.generate.ITYwDaObserveInfoService;
import com.eye.catcher.service.generate.ITYwDaTemperatureHumidityInfoService;
import com.eye.catcher.system.yw.Info.param.ObserveInfoDto;
import com.eye.catcher.system.yw.Info.param.ObserveInfoPage;
import com.eye.catcher.system.yw.Info.param.TemperatureHumidityInfoDto;
import com.eye.catcher.system.yw.Info.param.TemperatureHumidityInfoPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 温湿度 表 前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-05-12
 */
@Slf4j
@RestController
@RequestMapping("/eyeCatcher-system/temperature-humidity-info")
public class TYwDaTemperatureHumidityInfoController extends TSysDaBaseController {
    private final static String MODULE_CODE = "/temperature-humidity-info";
    private final static String MODULE_NAME = "温湿度";


    @Autowired
    private ITYwDaTemperatureHumidityInfoService infoService;

    @PostMapping("/addTemperatureHumidityInfo")
    @ResponseBody
    public ResponseData addTemperatureHumidityInfo(@RequestBody TYwDaTemperatureHumidityInfo info) {
        String methodCode = "/addTemperatureHumidityInfo";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,info.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            info.setCreateUserId(userId);
            this.infoService.addTemperatureHumidityInfo(info);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/editTemperatureHumidityInfo")
    @ResponseBody
    public ResponseData editTemperatureHumidityInfo(@RequestBody TYwDaTemperatureHumidityInfo info) {
        String methodCode = "/editTemperatureHumidityInfo";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_EDIT,info.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            info.setUpdateUserId(userId);
            this.infoService.editTemperatureHumidityInfo(info);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * @author huzhiwei
     * @param ids 项目id  多个id  ， 分割
     * @return
     */
    @PostMapping("/deleteTemperatureHumidityInfoByIds")
    @ResponseBody
    public ResponseData deleteTemperatureHumidityInfoByIds(@RequestParam(value = "ids") String ids) {
        String methodCode = "/deleteTemperatureHumidityInfoByIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_DELETE,"ids: "+ids,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.infoService.deleteByIds(ids);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    @PostMapping("/getAllList")
    @ResponseBody
    public ResponseData getAllList(@RequestBody TemperatureHumidityInfoDto temperatureHumidityInfo) {
        String methodCode = "/getAllList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,temperatureHumidityInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            temperatureHumidityInfo.setUserId(userId);
            commonResponse = CommonResponse.success();
            commonResponse.setData(this.infoService.getAllList(temperatureHumidityInfo));
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/getPageList")
    @ResponseBody
    public ResponseData getPageList(@RequestBody TemperatureHumidityInfoDto temperatureHumidityInfo) {
        String methodCode = "/getPageList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,temperatureHumidityInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            temperatureHumidityInfo.setUserId(userId);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.infoService.getPageList(
                            TemperatureHumidityInfoPage.builder()
                                    .temperatureHumidityInfo(temperatureHumidityInfo)
                                    .page(this.getPageInfo(temperatureHumidityInfo))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
}
